package org.dromara.system.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.SysMemberGroupBo;
import org.dromara.system.domain.vo.SysMemberGroupVo;
import org.dromara.system.service.ISysMemberGroupService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 会员分组
 *
 * @author Lion Li
 * @date 2025-09-18
 */

@RequiredArgsConstructor
@RestController
@RequestMapping("/system/memberGroup")
public class SysMemberGroupController extends BaseController {

    private final ISysMemberGroupService sysMemberGroupService;

    /**
     * 查询会员分组列表
     */
//    @SaCheckPermission("system:memberGroup:list")
    @GetMapping("/list")
    public TableDataInfo<SysMemberGroupVo> list(SysMemberGroupBo bo, PageQuery pageQuery) {
        return sysMemberGroupService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出会员分组列表
     */
//    @SaCheckPermission("system:memberGroup:export")
    @Log(title = "会员分组", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysMemberGroupBo bo, HttpServletResponse response) {
        List<SysMemberGroupVo> list = sysMemberGroupService.queryList(bo);
        ExcelUtil.exportExcel(list, "会员分组", SysMemberGroupVo.class, response);
    }

    /**
     * 获取会员分组详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("system:memberGroup:query")
    @GetMapping("/{id}")
    public R<SysMemberGroupVo> getInfo(@NotNull(message = "主键不能为空")
                                       @PathVariable Long id) {
        return R.ok(sysMemberGroupService.queryById(id));
    }

    /**
     * 新增会员分组
     */
//    @SaCheckPermission("system:memberGroup:add")
    @Log(title = "会员分组", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysMemberGroupBo bo) {
        return toAjax(sysMemberGroupService.insertByBo(bo));
    }

    /**
     * 修改会员分组
     */
//    @SaCheckPermission("system:memberGroup:edit")
    @Log(title = "会员分组", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated @RequestBody SysMemberGroupBo bo) {
        return toAjax(sysMemberGroupService.updateByBo(bo));
    }

    /**
     * 删除会员分组
     *
     * @param ids 主键串
     */
//    @SaCheckPermission("system:memberGroup:remove")
    @Log(title = "会员分组", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(sysMemberGroupService.deleteWithValidByIds(List.of(ids), true));
    }
}
